package com.chinabank.client.netty;


import com.chinabank.client.netty.BootNettyClientChannel;
import com.chinabank.client.netty.BootNettyClientChannelCache;
import io.netty.buffer.Unpooled;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 *  蚂蚁舞
 */
@Service
public class BootNettyHeartTimer {

    //  使用定时器发送心跳
//    @Scheduled(cron = "0/10 * * * * ?")
    public void heart_timer() {
        String back = "send heart";
        if(BootNettyClientChannelCache.channelMapCache.size() > 0){
            System.out.println(back);
            for (Map.Entry<String, BootNettyClientChannel> entry : BootNettyClientChannelCache.channelMapCache.entrySet()) {
                BootNettyClientChannel bootNettyChannel = entry.getValue();
                if(bootNettyChannel != null && bootNettyChannel.getChannel().isOpen()){
                    bootNettyChannel.getChannel().writeAndFlush(Unpooled.buffer().writeBytes(back.getBytes()));
                }
            }
        }

    }

}